<template>
  <div>
    <!-- 面包屑导航区域 -->
    <div class="el-header">
      <el-menu background-color="#eee" text-color="#303133" class="el-menu-demo" mode="horizontal">
        <el-menu-item index="/home" @click="$router.push('/home')">首页</el-menu-item>
        <el-menu-item>提现管理</el-menu-item>
      </el-menu>
    </div>
    <!-- 面包屑导航区域 -->
    <el-card>
      <!-- 表单-条件查询区域 -->
      <el-form ref="form" :model="form" label-width="100px" inline sizi="mini">
        <el-form-item label="名称/昵称" prop="username">
          <el-input v-model.trim="form.username"></el-input>
        </el-form-item>
        <el-form-item label="店铺/用户ID" prop="user_id">
          <el-input v-model.trim="form.user_id"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="withdrawal_status">
          <el-input v-model.trim="form.withdrawal_status"></el-input>
        </el-form-item>
        <el-form-item label="提现单号" prop="withdrawal_order">
          <el-input v-model.trim="form.withdrawal_order"></el-input>
        </el-form-item>
        <el-form-item label="申请提现日期" prop="withdrawal_time">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.date1"></el-date-picker>
          </el-col>
          <el-col class="text" :span="2">至</el-col>
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.date2"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 表单-条件查询区域 -->
      <!-- 数据表格展示区域 -->
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column prop="transaction_type" label="交易类型" width="80px"> </el-table-column>
        <el-table-column prop="user_id" label="店铺ID/用户ID"> </el-table-column>
        <el-table-column prop="username" label="店铺名称/昵称"> </el-table-column>
        <el-table-column prop="withdrawal_order" label="提现单号" width="80px"> </el-table-column>
        <el-table-column prop="withdrawal_amount" label="提现金额" width="80px"> </el-table-column>
        <el-table-column prop="apply_time" label="申请提现时间"> </el-table-column>
        <el-table-column prop="withdrawal_status" label="状态" width="80px">
          <template slot-scope="scope">
            <span>{{ scope.row.withdrawal_status === true ? '已提现' : '待处理' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="withdrawal_time" label="提现时间"> </el-table-column>
        <el-table-column prop="withdrawal_note" label="提现备注"> </el-table-column>
        <el-table-column label="操作" width="80px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" v-show="!scope.row.withdrawal_status" @click="onHandler({ row: scope.row, flag: true })">处理</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格区域 -->
      <!-- 点击处理对话框 -->
      <el-dialog v-if="dialogVisible" title="提示" :visible.sync="dialogVisible" width="30%">
        <span>确认{{ row.username }}提现申请打款</span>
        <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="withdrawal_note"> </el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onHandler({ flag: false })">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 点击处理对话框 -->
    </el-card>
  </div>
</template>
<script>
import dayjs from 'dayjs'
export default {
  name: 'CashManagement',
  data() {
    return {
      form: {
        username: '',
        user_id: '',
        withdrawal_status: '',
        withdrawal_order: '',
        date1: '',
        date2: ''
      },
      tableData: [],
      dialogVisible: false,
      row: null,
      withdrawal_note: ''
    }
  },
  created() {
    this.getCashManagement()
  },
  methods: {
    async getCashManagement() {
      const { data: res } = await this.$http.get('/cashManagements')
      console.log(res)
      this.tableData = res
    },
    async onHandler(obj) {
      if (obj.flag === true) {
        this.row = obj.row
        this.dialogVisible = true
      } else {
        console.log(obj)
        this.row.withdrawal_status = true
        this.row.withdrawal_note = this.withdrawal_note
        const { data: res } = await this.$http.put(`/cashManagements/${this.row.id}`, this.row)
        console.log(res)
        this.dialogVisible = false
      }
    },
    async submitForm() {
      // 全部都没有填写 条件.,显示所有数据
      var str = ''
      this.tableData = []
      for (var k in this.form) {
        // console.log(this.form[k] instanceof Date)
        str += this.form[k]
        if (this.form[k]) {
          // 发送请求获取数据
          console.log(k, this.form[k])
          const { data: res } = await this.$http.get(`/cashManagements?${k}_like=${this.form[k]}`)
          res.forEach(item => {
            if (!this.tableData.find(subitem => subitem.id === item.id)) {
              this.tableData.push(item)
            }
          })
        }
      }

      if (this.form.date1 || this.form.date2) {
        console.log(222)
        var startDate = ''
        var endDate = ''
        if (this.form.date1) {
          startDate = dayjs(this.form.date1).format('YYYY-MM-DD')
        }
        if (this.form.date2) {
          endDate = dayjs(this.form.date2).format('YYYY-MM-DD')
        }
        console.log(startDate, endDate)

        var filter = startDate ? 'withdrawal_time_gte=' + startDate : ''
        if (filter === '') {
          filter += endDate ? 'withdrawal_time_lte=' + endDate : ''
        } else {
          filter += endDate ? '&withdrawal_time_lte=' + endDate : ''
        }

        const { data: res } = await this.$http.get(`/cashManagements?${filter}`)
        res.forEach(item => {
          if (!this.tableData.find(subitem => subitem.id === item.id)) {
            this.tableData.push(item)
          }
        })
      }
      console.log(str)
      if (!str || str === 'null') {
        const { data: res } = await this.$http.get('/cashManagements')
        this.tableData = res
      }
      // const { data: res } = await this.$http.get('/cashManagements/', )
      // console.log(res)
    },
    resetForm() {
      console.log(this.$refs.form)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-top: 15px;
  margin-left: 15px;
}
.el-header {
  padding: 0;
}
.el-card {
  margin: 25px;
  min-width: 1200px im !important;
  .el-form {
    .el-button {
      margin-left: 30px;
    }
    .text {
      padding-left: 15px;
    }
  }
}
</style>
